package com.echo.fm.content.news.type;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.echo.fm.admin.auto.model.Admin;
import com.echo.fm.content.news.type.model.NewsType;
import com.echo.util.ErrorException;
import com.echo.util.JSONResult;
import com.echo.util.ResultUtils;
import com.echo.util.TimeGetter;
import com.echo.util.impl.HeaderParser;
import com.echo.util.impl.ValidateUtils;

@Controller
public class INewsTypeController {
	@Autowired
	@Qualifier("newsTypeService")
	INewsTypeService newsTypeService;

	@RequestMapping(value = "/web/newsType/guide")
	public String NewsType(HttpServletRequest request, HttpServletResponse response) {
		return "/pages/content/news/news-add";
	}

	@RequestMapping(value = "/web/newsType/createSavePage")
	public String addNewsTypePage(HttpServletRequest request, HttpServletResponse response) {
		return "/pages/content/news/news-category";
	}
	
	@RequestMapping(value = "/web/newsType/addSave", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	Object addNewsType(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			Admin detail = HeaderParser.getUser(request);
			if (detail == null) {
				return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			}
			NewsType newsType = new NewsType();
			String newsTypeStr = request.getParameter("title");
			if (ValidateUtils.isNotEmpty(newsTypeStr)) {
				newsType.setTitle(newsTypeStr);
			}
			String content = request.getParameter("content");
			if (ValidateUtils.isNotEmpty(content)) {
				newsType.setContent(content);
			}
			String oper = request.getParameter("oper");
			if (ValidateUtils.isNotEmpty(oper)) {
				newsType.setOper(oper);
			}
			String status = request.getParameter("status");
			if (ValidateUtils.isNotEmpty(status)) {
				if ("on".equals(status)) {
					newsType.setStatus(1);
				} else {
					newsType.setStatus(0);
				}
			}
			newsType.setT(TimeGetter.getTime());
			newsTypeService.addNewsType(newsType);
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

	@RequestMapping(value = "/web/newsType/list", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	Object listNewsType(HttpServletRequest request, HttpServletResponse response) {
		try {
			Admin detail = HeaderParser.getUser(request);
			if (detail == null) {
				return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			}
			NewsType newsType = new NewsType();
			List<NewsType> newsTypeList = newsTypeService.searchNewsTypeList();			
			Map<String, List<NewsType>> map = new HashMap<String,List<NewsType>>();
			map.put("list", newsTypeList);
			JSONResult result = ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
			result.setData(map);
			return result;
		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}
	}

	@RequestMapping(value = "web/newsType/deletSave")
	@ResponseBody
	public Object deleteNewsType(HttpServletRequest request, HttpServletResponse response) {
		try {
			Admin detail = HeaderParser.getUser(request);
			if (detail == null) {
				throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			}
			String id = request.getParameter("id");
			NewsType newsType = newsTypeService.searchNewsTypeId(Integer.valueOf(id));
			if (newsType != null) {
				newsTypeService.deleteNewsType(newsType);
			}
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
		} catch (ErrorException e) {
			e.printStackTrace();
			return new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

	@RequestMapping(value = "web/newsType/updateSave")
	@ResponseBody
	Object updateNewsType(HttpServletRequest request, HttpServletResponse response) {
		try {
			Admin detail = HeaderParser.getUser(request);
			if (detail == null) {
				return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
			}
			String id = request.getParameter("id");
			NewsType newsTypeExist = newsTypeService.searchNewsTypeId(Integer.valueOf(id));
			if (newsTypeExist != null) {
				String title = request.getParameter("title");
				if (ValidateUtils.isNotEmpty(title)) {
					newsTypeExist.setTitle(title);
				}
				String content = request.getParameter("content");
				if (ValidateUtils.isNotEmpty(content)) {
					newsTypeExist.setContent(content);
				}
				String oper = request.getParameter("oper");
				if (ValidateUtils.isNotEmpty(oper)) {
					newsTypeExist.setOper(oper);
				}
				
				String status= request.getParameter("isShow");
				if(ValidateUtils.isNotEmpty(status)){
					if(Integer.valueOf(status)==0){
						newsTypeExist.setStatus(0);
					}else {
						newsTypeExist.setStatus(1);
					}
				}								
				newsTypeExist.setT(TimeGetter.getTime());
				newsTypeService.updateNewsType(newsTypeExist);
			}
			return ResultUtils.instJSONResult(ResultUtils.WEB_SUCCESS_CODE);
		} catch (ErrorException e) {
			e.printStackTrace();
			return ResultUtils.instJSONResult(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		}

	}

}
